#ifndef __MY_HOT__
#define __MY_HOT__
#include <unistd.h>
#include "data.hpp"
extern cloud::DataManager *_data;

namespace cloud
{
    
    class HotManager
    {
        public:
            HotManager()
            {
                Config *config = Config::GetInstance();
                _back_dir = config->GetBackDir();
                _pack_dir = config->GetPackDir();
                _hot_time = config->GetHotTime();
                FileUtil tmp1(_back_dir);
                FileUtil tmp2(_pack_dir);
                if(tmp1.Exists() == false) tmp1.CreateDirectory();
                if(tmp2.Exists() == false) tmp2.CreateDirectory();
            }
            bool RunModule()
            {   
                while(1)
                {
                    FileUtil fu(_back_dir);
                    std::vector<std::string> arry;
                    fu.ScanDirectory(&arry);
                    for(auto &e : arry)
                    {
                        bool ret = HotJudge(e);
                        if(!ret) 
                        { // 非热点文件，压缩存储并删除原文件
                            FileUtil tmp(e);
                            BackupInfo bi;
                            if(_data->GetOneByRealPath(e, &bi) == false)
                            {
                                bi.NewBackupInfo(e);
                                //std::cout << e << std::endl; 
                            } 
                            tmp.Compress(bi.packpath);
                            tmp.Remove();
                            bi.pack_flag = true;
                            //std::cout << bi.url << std::endl;
                            _data->Update(bi);

                        }else 
                        { // 热点文件暂时不做处理
                        }
                    }
                    usleep(1000);
                }
                
            }
        private:
            bool HotJudge(std::string filename)
            {
                FileUtil fu(filename);
                time_t last_atime = fu.LastATime();
                time_t cur_time = time(NULL);
                if(cur_time - last_atime > _hot_time) return false; // 非热点文件返回false
                return true;
            }
        private:
            std::string _back_dir;
            std::string _pack_dir;
            int _hot_time;


    };

}

#endif